package br.edu.fa7.receita.ui;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import br.edu.fa7.receita.R;

public class ListaReceitasActivity extends ActivityGroup {

	int valorId = 0;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lista_receitas);
		
		valorId = getIntent().getExtras().getInt("idUsuario");
		
		TabHost tabHost = (TabHost) findViewById(R.id.tabHost);
		tabHost.setup(getLocalActivityManager());

		TabSpec aba1 = tabHost.newTabSpec("foo1");
		aba1.setIndicator("Minhas Receitas");
		Intent intent = new Intent(ListaReceitasActivity.this, MinhasReceitasActivity.class);
		intent.putExtra("idUsuarioLogado", valorId);
		aba1.setContent(intent);
		tabHost.addTab(aba1);
		
		TabSpec aba2 = tabHost.newTabSpec("foo2");
		aba2.setIndicator("Todas Receitas");
		aba2.setContent(new Intent(this, TodasReceitasActivity.class));
		tabHost.addTab(aba2);
		
		tabHost.setCurrentTab(0);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_lista_receitas, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		
		case R.id.menu_search:
			break;

		case R.id.menu_add:
			Intent intent = new Intent(ListaReceitasActivity.this, CadastroReceitaActivity.class);
			intent.putExtra("idUsuario", valorId);
			startActivityForResult(intent, 0);
			break;
		}
		
		return super.onOptionsItemSelected(item);
	}
	
	private void atualizarLista(int idUsuario){
		
		Intent intent = new Intent(ListaReceitasActivity.this, ListaReceitasActivity.class);
		intent.putExtra("idUsuario", idUsuario);
		startActivity(intent);
		
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		int idUsuario = data.getExtras().getInt("idUsuarioLogado");
	  	
	  	if(requestCode == 0 && resultCode == RESULT_OK) {
	  		atualizarLista(idUsuario);
	  	}
	}
}
